﻿using System;

namespace UserManagementSystem
{
    public class User
    {
        private string username;
        private string password;
        private string email;
        
        public string Username
        {
            get { return username; }
            set 
            { 
                if (string.IsNullOrWhiteSpace(value))
                    throw new ArgumentException("用户名不能为空");
                username = value; 
            }
        }

        public string Password
        {
            get { return password; }
            set 
            { 
                if (string.IsNullOrWhiteSpace(value) || value.Length < 6)
                    throw new ArgumentException("密码不能少于6位");
                password = value; 
            }
        }

        public string Email
        {
            get { return email; }
            set 
            { 
                if (string.IsNullOrWhiteSpace(value) || !value.Contains("@"))
                    throw new ArgumentException("邮箱格式不正确");
                email = value; 
            }
        }
        
        public User() { }

        public User(string username, string password, string email)
        {
            Username = username;
            Password = password;
            Email = email;
        }
        
        public override string ToString()
        {
            return $"用户名: {Username}, 邮箱: {Email}";
        }
        
        public override bool Equals(object obj)
        {
            if (obj is User other)
            {
                return Username == other.Username;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return Username?.GetHashCode() ?? 0;
        }
    }
}